package com.catjiu.webeditor.parser;

import cn.hutool.core.bean.BeanUtil;
import com.catjiu.webeditor.ParserContext;
import com.catjiu.webeditor.style.RStyle;
import com.catjiu.webeditor.style.Style;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

public class HerfNodeParser extends NodeParser{
    @Override
    public void parser(ParserContext context, Style style) {
        RStyle rStyle = style==null?new RStyle(): (RStyle) style;
        context.buildHyperlinkStype();
        Element node = (Element) getNode();
        String href = node.attr("href");
        PackageRelationship relationship = context.getWord().getPackagePart().addExternalRelationship(href, XWPFRelation.HYPERLINK.getRelation());
        context.createHyperlink(relationship.getId());
        //切换run节点的父节点类型
        context.setRpt(ParserContext.RPT.HYPERLINK);
        for (Node child : getNode().childNodesCopy()) {
            ParserSelector.select(child).parser(context, BeanUtil.toBean(rStyle, RStyle.class));
        }
        //切换rpt上下文
        context.setRpt(null);
    }
}
